package com.pig4cloud.pigx.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.system.constant.ArticleTypeEnum;
import com.pig4cloud.pigx.system.dto.ArticleInformationDTO;
import com.pig4cloud.pigx.system.entity.ArticleInformation;
import com.pig4cloud.pigx.system.service.ArticleManagementService;
import com.pig4cloud.pigx.system.vo.EmailInformationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Description: 文章管理
 *
 * @Date: 2022/8/19 19:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/articleManagement" )
@Api(value = "articleManagement", tags = "文章管理")
public class ArticleManagementController {

	private final ArticleManagementService articleManagementService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param articleInformation 文章列表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page" )
	public R getArticlePage(Page page, ArticleInformation articleInformation) {
		page.setOptimizeCountSql(false);
		page.setSearchCount(false);
		//根据创建时间逆序排序
		HashMap<String, Object> map = new HashMap<>();
		map.put("field", "create_time");
		map.put("order", "DESC");
		return articleManagementService.page(page, articleInformation, map);
	}


	/**
	 * 通过id查询文章列表
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	public R getById(@PathVariable("id" ) Long id) {
		return articleManagementService.getArticleById(id);
	}

	/**
	 * 新增文章列表
	 * @param articleInformation 文章列表
	 * @return R
	 */
	@ApiOperation(value = "新增文章", notes = "新增文章")
	@SysLog("新增文章" )
	@PostMapping
	public R save(@RequestBody ArticleInformation articleInformation) {
		return articleManagementService.saveArticleInformation(articleInformation);
	}

	/**
	 * 修改文章列表
	 * @param articleInformation 文章列表
	 * @return R
	 */
	@ApiOperation(value = "修改文章", notes = "修改文章")
	@SysLog("修改文章" )
	@PutMapping
	public R updateByIdAndLanguage(@RequestBody ArticleInformation articleInformation) {
		return articleManagementService.updateById(articleInformation);
	}

	/**
	 * 通过id删除文章列表
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除文章列表", notes = "通过id删除文章列表")
	@SysLog("通过id删除文章列表" )
	@DeleteMapping("/{id}" )
	public R removeById(@PathVariable Long id) {
		return articleManagementService.removeById(id);
	}


	/**
	 * 获取文章详情
	 * @param articleInformation 文章列表
	 * @return
	 */
	@Inner(value = false)
	@ApiOperation(value = "获取文章详情", notes = "获取文章详情")
	@PostMapping("/getArticleManagement" )
	public R getArticleManagement(@RequestBody ArticleInformation articleInformation) {
		//根据创建时间逆序排序
		HashMap<String, Object> map = new HashMap<>();
		map.put("field", "create_time");
		if (articleInformation.getArticleTypeCode().equals(ArticleTypeEnum.PROCLAMATION.getId())){
			map.put("order", "DESC");
		} else {
			map.put("order", "ASC");
		}

		if (articleInformation.getLanguageId() == null || articleInformation.getLanguageId().isEmpty()) {
			articleInformation.setLanguageId(LocaleContextHolder.getLocale().toString());
		}
		return articleManagementService.getArticleManagement(articleInformation, map);
	}

	/**
	 * 获取文章详情 Feign接口调用
	 * @param articleInformation 文章类型判断条件
	 * @return
	 */
	@Inner
	@ApiOperation(value = "获取文章详情 Feign接口调用", notes = "获取文章详情 Feign接口调用 @RequestBody：articleTypeCode， languageId")
	@PostMapping("/getArticleManagementFeign" )
	public R<List<ArticleInformation>> getArticleManagementFeign(@RequestBody ArticleInformation articleInformation) {
		return articleManagementService.getArticleManagement(articleInformation, null);
	}

	/**
	 * 获取邮件内容详情 Feign接口调用
	 * @param articleInformationDTO 文章类型判断条件
	 * @return
	 */
	@Inner
	@ApiOperation(value = "获取邮件内容详情", hidden = true)
	@PostMapping("/getEmailManagementFeign" )
	public R<List<EmailInformationVO>> getEmailManagementFeign(@RequestBody ArticleInformationDTO articleInformationDTO) {
		return articleManagementService.getEmailManagement(articleInformationDTO, null);
	}

}

